package com.cc.moxingvoice.ui.main;

import android.app.Dialog;
import android.content.Context;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.view.View;

import com.cc.moxingvoice.R;
import com.cc.moxingvoice.base.BaseActivity;
import com.cc.moxingvoice.constant.LocalPath;
import com.cc.moxingvoice.databinding.ItemVoicePackageBinding;
import com.cc.moxingvoice.model.VoicePackageItem;
import com.cc.moxingvoice.ui.voice.AddVoicePackageActivity;
import com.cc.moxingvoice.utils.FilesUtil;
import com.cc.moxingvoice.widget.dialog.ButtonTwoDialog;
import com.superc.cframework.base.ui.CBaseRecyclerViewAdapter;

import java.util.List;

/**
 * 创建日期：2018/2/23 on 14:54
 * 描述：
 * 作者：郭士超
 * QQ：1169380200
 */

public class VoicePackageAdapter extends CBaseRecyclerViewAdapter<VoicePackageItem> {

    /**
     * 构造函数
     *
     * @param context    上下文
     * @param dataList   数据列表
     * @param layoutId   单布局
     * @param variableId DataBinding的BR
     */
    public VoicePackageAdapter(Context context, List<VoicePackageItem> dataList, int layoutId, int variableId) {
        super(context, dataList, layoutId, variableId);
    }

    @Override
    protected void subTask(ViewDataBinding binding, final int position) {
        super.subTask(binding, position);
        // 这里可以写自己一些自己的逻辑，如果不需要，一般直接用BaseListViewAdapter就可以，不需要再构建

        // 点击删除按钮
        ((ItemVoicePackageBinding)binding).delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDeleteDialog(position);
            }
        });

        // 点击修改按钮
        ((ItemVoicePackageBinding)binding).edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putBoolean("edit", true);
                bundle.putString("title", mDataList.get(position).getTitle());
                ((BaseActivity)mContext).startActivity(AddVoicePackageActivity.class, bundle);
            }
        });
    }

    private void showDeleteDialog(final int position){
        new ButtonTwoDialog(mContext, R.style.dialog, mContext.getString(R.string.are_you_sure_to_delete_this_voice_package), new ButtonTwoDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if(confirm){
                    String path = LocalPath.VOICE_PATH + mDataList.get(position).getTitle();
                    FilesUtil.delete(path);
                    mDataList.remove(position);
                    VoicePackageAdapter.this.notifyDataSetChanged();
                    dialog.dismiss();
                }
            }
        }).setTitle(mContext.getString(R.string.hint)).show();
    }

}
